//
//  XMGTabBarController.m
//  01 baisi
//
//  Created by 檀志文 on 16/2/11.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "XMGTabBarController.h"
#import "XMGEssenceViewCongtroller.h"
#import "XMGFriendTrendsViewController.h"
#import "XMGMeViewController.h"
#import "XMGNewViewController.h"

#import "XMGNavigationController.h"

#import "XMGTabBar.h"

@implementation XMGTabBarController

+(void) initialize{
    //通过appearance 统一设置所有UITabBarItem的文字属性
//    后面带有UI_APPEARANCE_SELECTOR 的方法，都可以通过appearance对象来统一设置
    
//    选择不同的富文本的样式和字体（选中和非选中的状态下）
    
    NSMutableDictionary *attrs= [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName]=[UIColor grayColor];
//
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName]=[UIColor darkGrayColor];
    
//    设置 tabbar 中的item 全局的样式
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateHighlighted];
    
}

-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setupChildVc:[[XMGEssenceViewCongtroller alloc]init] title:@"精华" image:@"tabBar_essence_icon"selectedImage:@"tabBar_essence_click_icon"];
    
    [self setupChildVc:[[XMGFriendTrendsViewController alloc]init] title:@"关注" image:@"tabBar_friendTrends_icon"selectedImage:@"tabBar_friendTrends_click_icon"];
    
    [self setupChildVc:[[XMGNewViewController alloc]init] title:@"新帖" image:@"tabBar_new_icon"selectedImage:@"tabBar_new_click_icon"];
    
    [self setupChildVc:[[XMGMeViewController alloc]init] title:@"我" image:@"tabBar_me_icon"selectedImage:@"tabBar_me_click_icon"];
//    更换tabBar
    [self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
}

//初始化子控制器
-(void) setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
//    设置文字和图片
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    XMGNavigationController *nav = [[XMGNavigationController alloc]initWithRootViewController:vc];
//    添加到子控制器
    [self addChildViewController:nav];
}

@end
